<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="电梯id" prop="elevId">
        <el-input
          v-model="queryParams.elevId"
          placeholder="请输入电梯id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电梯工号" prop="factoryNo">
        <el-input
          v-model="queryParams.factoryNo"
          placeholder="请输入电梯工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="完成状态" prop="finishStatus">
        <el-select v-model="queryParams.finishStatus" placeholder="请选择完成状态" clearable size="small">
          <el-option v-for="item in finishStatusList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="维修类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择维修类型" clearable size="small">
          <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['security:afElevatorRepair:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['security:afElevatorRepair:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['security:afElevatorRepair:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['security:afElevatorRepair:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleSync"
          v-hasPermi="['security:afElevatorRepair:sync']"
        >同步</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afElevatorRepairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="维修工单号" align="center" prop="orderNo" />
      <el-table-column label="电梯id" align="center" prop="elevId" />
      <el-table-column label="电梯工号" align="center" prop="factoryNo" />
      <el-table-column label="内部编号" align="center" prop="aliasOfAddress" />
      <el-table-column label="工单创建人" align="center" prop="creater" />
      <el-table-column label="使用单位名称" align="center" prop="customerCompanyName" />
      <el-table-column label="故障原因" align="center" prop="faultReason" />
      <el-table-column label="完成状态" align="center" prop="finishStatus" />
      <el-table-column label="维修时长(m)" align="center" prop="repairDuration" />
      <el-table-column label="维保人" align="center" prop="maintainerOneName" />
      <el-table-column label="维保人电话" align="center" prop="maintainerOnePhone" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="维修日期" align="center" prop="repairDate" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="维修类型" align="center" prop="type" />
      <el-table-column label="维保单位名称" align="center" prop="upkeepCompanyName" />
      <el-table-column label="最后更新时间" align="center" prop="lastUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['security:afElevatorRepair:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['security:afElevatorRepair:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电梯维修单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="工单号" prop="orderNo">-->
          <!--<el-input-->
            <!--v-model="form.orderNo"-->
            <!--placeholder="请输入维修工单号"-->
          <!--/>-->
        <!--</el-form-item>-->
        <el-form-item label="电梯id" prop="elevId">
          <el-input v-model="form.elevId" placeholder="请输入电梯id" />
        </el-form-item>
        <el-form-item label="电梯工号" prop="factoryNo">
          <el-input v-model="form.factoryNo" placeholder="请输入电梯工号" />
        </el-form-item>
        <el-form-item label="内部编号" prop="aliasOfAddress">
          <el-input v-model="form.aliasOfAddress" placeholder="请输入内部编号" />
        </el-form-item>
        <el-form-item label="工单创建人" prop="creater">
          <el-input v-model="form.creater" placeholder="请输入工单创建人" />
        </el-form-item>
        <el-form-item label="使用单位名称" prop="customerCompanyName">
          <el-input v-model="form.customerCompanyName" placeholder="请输入使用单位名称" />
        </el-form-item>
        <el-form-item label="故障原因" prop="faultReason">
          <el-input v-model="form.faultReason" placeholder="请输入故障原因" />
        </el-form-item>
        <el-form-item label="完成状态">
          <el-select v-model="form.finishStatus" placeholder="请选择完成状态" clearable size="small">
            <el-option v-for="item in finishStatusList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="维修时长(m)" prop="repairDuration">
          <el-input v-model="form.repairDuration" placeholder="请输入维修时长(m)" />
        </el-form-item>
        <el-form-item label="维保人" prop="maintainerOneName">
          <el-input v-model="form.maintainerOneName" placeholder="请输入维保人" />
        </el-form-item>
        <el-form-item label="维保人电话" prop="maintainerOnePhone">
          <el-input v-model="form.maintainerOnePhone" placeholder="请输入维保人电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="维修日期" prop="repairDate">
          <el-input v-model="form.repairDate" placeholder="请输入维修日期" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态" clearable size="small">
            <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="维修类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择维修类型" clearable size="small">
            <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="维保单位名称" prop="upkeepCompanyName">
          <el-input v-model="form.upkeepCompanyName" placeholder="请输入维保单位名称" />
        </el-form-item>
        <el-form-item label="最后更新时间" prop="lastUpdateTime">
          <el-date-picker clearable size="small"
            v-model="form.lastUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAfElevatorRepair, getAfElevatorRepair, delAfElevatorRepair, addAfElevatorRepair, updateAfElevatorRepair,syncAfElevatorRepair } from "@/api/smartParks/security/afElevatorRepair";

export default {
  name: "AfElevatorRepair",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电梯维修单表格数据
      afElevatorRepairList: [],
      finishStatusList:[{
        value: '1',
        label: '未完成'
      }, {
        value: '2',
        label: '超期完成'
      }, {
        value: '3',
        label: '提前完成'
      }, {
        value: '4',
        label: '正常'
      }],
      statusList:[{
        value: '1',
        label: '计划中'
      },{        value: '2',
        label: '未接受'},{value: '3',
        label: '已接受'},{value: '4',
        label: '已签到'},{value: '5',
        label: '已完成'},{value: '6',
        label: '签字完成'},{value: '7',
        label: '作废'}],
      typeList:[{
        value: '1',
        label: '困人急修'
      },{        value: '2',
        label: '非困人急修'},{value: '3',
        label: '计划维修'}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        elevId: null,
        factoryNo: null,
        aliasOfAddress: null,
        creater: null,
        customerCompanyName: null,
        faultReason: null,
        finishStatus: null,
        repairDuration: null,
        maintainerOneName: null,
        maintainerOnePhone: null,
        repairDate: null,
        status: null,
        type: null,
        upkeepCompanyName: null,
        lastUpdateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询电梯维修单列表 */
    getList() {
      this.loading = true;
      listAfElevatorRepair(this.queryParams).then(response => {
        // response.rows.forEach((item) => {
        //   this.statusList.forEach((itemType) => {
        //     if (itemType.value === item.status) {
        //       item.statusNew = itemType.label;
        //     }
        //   });
        //   this.typeList.forEach((itemType) => {
        //     if (itemType.value === item.type) {
        //       item.typeNew = itemType.label;
        //     }
        //   });
        //   this.finishStatusList.forEach((itemType) => {
        //     if (itemType.value === item.finishStatus) {
        //       item.finishStatusNew = itemType.label;
        //     }
        //   });
        // });
        this.afElevatorRepairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderNo: null,
        elevId: null,
        factoryNo: null,
        aliasOfAddress: null,
        creater: null,
        customerCompanyName: null,
        faultReason: null,
        finishStatus: 0,
        repairDuration: null,
        maintainerOneName: null,
        maintainerOnePhone: null,
        remark: null,
        repairDate: null,
        status: 0,
        type: null,
        upkeepCompanyName: null,
        createTime: null,
        lastUpdateTime: null
      };
      this.resetForm("form");
    },
    handleSync(){
      this.loading = true;
      syncAfElevatorRepair().then(response => {
        this.getList();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderNo)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电梯维修单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderNo = row.orderNo || this.ids
      getAfElevatorRepair(orderNo).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电梯维修单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderNo != null) {
            updateAfElevatorRepair(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAfElevatorRepair(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderNos = row.orderNo || this.ids;
      this.$confirm('是否确认删除电梯维修单编号为"' + orderNos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAfElevatorRepair(orderNos);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('security/afElevatorRepair/export', {
        ...this.queryParams
      }, `security_afElevatorRepair.xlsx`)
    }
  }
};
</script>
